﻿/**
* QSmart Andon
*
* 2012-10-11
* 
* steven.li
*
* Copyright(c) 2012 All Rights Reserved. Taiyou Tech(GUANGZHOU) Co.,Ltd.  (广州太友科技) [ sales@gztaiyou.com]. 
*
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using FX.CC.Andon.Entity.SystemManage;
using FX.CC.Andon.Service.SystemManage;
using FX.CC.Andon.Utility.Json;

namespace FX.CC.Andon.Business.SystemManage
{
    public class RoleRightBusiness
    {
        private readonly RoleRightService _dal = new RoleRightService();

        public bool SetRoleRight(string data)
        {
            var result = false;
            var model = new RoleRightEntity();
            var list = new List<RoleRightEntity>();
            var rows = (ArrayList)JsonHelper.Decode(data);
            foreach (Hashtable row in rows)
            {
                model.RoleID = new Guid(row["RoleID"].ToString());
                model.MenuIds = row["menuIds"].ToString();
            }
            if (!string.IsNullOrEmpty(model.MenuIds))
            {
                var sArray = model.MenuIds.Split(',');
                list.AddRange(sArray.Select(t => new RoleRightEntity { RightId = Guid.NewGuid(), RoleID = new Guid(model.RoleID.ToString()), MenuID = new Guid(t) }));
                if (_dal.AddRoleRight(model.RoleID.ToString(), list))
                {
                    result = true;
                }
            }
            else
            {
                if (_dal.RemoveRoleRightByRoleId(model.RoleID.ToString()))
                {
                    result = true;
                }
            }
            return result;
        }

        public string GetMenuIdForRoleRight(string roleId)
        {
            var strValue = string.Empty;
            var list = _dal.GetMenuIdForRoleRight(roleId);
            if (list.Count > 0)
            {
                for (var i = 0; i < list.Count; i++)
                {
                    var value = list[i].MenuID.ToString();

                    if (i != list.Count - 1)
                    {
                        strValue += value + ",";
                    }
                    else
                    {
                        strValue += value;
                    }

                }
            }
            return strValue;
        }
    }
}
